// pages/doortodoor/doortodoor.js
import { placeorderpage,appointmentTime,getRegion,getTwoChannel,getDoorPrice,createOrder } from '../../api/service'
import { delayPay } from '../../api/order'
import { getTip } from '../../api/appointment'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: true,
    title: '上门预约',
    opinionImages: '',
    classifyListOne: [],
    classifyListtwo: [],
    days: [],
    shenRegion: [],
    oneindex: 0,
    twoindex: 0,
    rankindex: 1,
    dayindex: 0,
    times: [],
    timeIndex: 0,
    privince: [],
    city: [],
    area: [],
    privinceId: -1,
    cityId: -1,
    areaId: -1,
    privincetext: '请选择省',
    citytext: '请选择市',
    areatext: '请选择区',
    imglist: [],
    oneid: "",
    price: -1,
    address: "",
    contacts: "",
    mobile: "",
    remark: "",
    detailText: ''
  },
  uploadImgs(e){
    var imgs = e.detail.picsList.join(",")
    this.setData({opinionImages:imgs})
  },
  getInitCon() {
    placeorderpage().then(res => {
      this.setData({
        classifyListOne:res.data.classifyListOne,
        classifyListtwo:res.data.classifyListtwo,
        days: res.data.days,
        shenRegion: res.data.shenRegion
      })
      const time = res.data.days[0].appointmentDate
      appointmentTime(time).then(res => {
        this.setData({
          times: res.data.timeList,
          dayindex: 0,
          timeIndex: 0
        })
      })
      this.getPrice()
    })
  },
  switchOneIndex(e) {
    const index = e.currentTarget.dataset.index
    console.log(index)
    console.log(this.data.classifyListOne)
    this.setData({
      oneindex: index,
      oneid: this.data.classifyListOne[index].id
    })
    this.getTwoProject()
  },
  switchTwoIndex(e) {
    const index = e.currentTarget.dataset.index
    this.setData({
      twoindex: index
    })
  },
  switchRankIndex(e) {
    const index = e.currentTarget.dataset.index
    this.setData({
      rankindex: index
    })
    this.getPrice()
  },
  swichDaysIndex(e) {
    const index = e.currentTarget.dataset.index
    const item = e.currentTarget.dataset.item
    const time = item.appointmentDate
    // this.setData({
    //   dayindex: index
    // })
    appointmentTime(time).then(res => {
      this.setData({
        times: res.data.timeList,
        dayindex: index,
        timeIndex: 0
      })
    })
  },
  switchTimeIndex(e) {
    const index = e.currentTarget.dataset.index
    this.setData({
      timeIndex: index
    })
  },
  bindPrivincePickerChange(e) {
    const index = e.detail.value
    const id = this.data.privince[index].id
    this.setData({
      privinceId: id,
      privincetext: this.data.privince[index].name
    })
    //清空市区
    this.setData({
        citytext: '请选择市',
        cityId: -1,
        city: [],
        areatext: '请选择区',
        areaId: -1,
        area: []
    })
    this.getCity()
  },
  bindCityPickerChange(e) {
    const index = e.detail.value
    const id = this.data.city[index].id
    this.setData({
      cityId: id,
      citytext: this.data.city[index].name
    })
    //清空区
    this.setData({
        areatext: '请选择区',
        areaId: -1,
        area: []
    })
    this.getArea()
  },
  bindAreaPickerChange(e) {
    const index = e.detail.value
    const id = this.data.area[index].id
    this.setData({
      areaId: id,
      areatext: this.data.area[index].name
    })
  },
  getCity() {
    getRegion({pid: this.data.privinceId}).then(res => {
      // const id = this.data.privinceId
      // const city = []
      // res.data.forEach(item => {
      //   if(item.pid == id) {
      //     city.push(item)
      //   }
      // })
      this.setData({
        city: res.data
      })
    })
  },
  getArea() {
    getRegion({pid: this.data.cityId}).then(res => {
      // const id = this.data.cityId
      // const area = []
      // res.data.forEach(item => {
      //   if(item.pid == id) {
      //     area.push(item)
      //   }
      // })
      this.setData({
        area: res.data
      })
    })
  },
  getTwoProject() {
    const data = {
      parentId: this.data.oneid,
      state: "Y",
      showWechat: "Y",
      del_state: "N"
    }
    getTwoChannel(data).then(res => {
      this.setData({
        classifyListtwo: res.data
      })
      this.getPrice()
    })
  },
  getPrice() {
    //getDoorPrice
    const data = {
      classifyId: this.data.classifyListOne[this.data.oneindex].id,
      classifyCid: this.data.classifyListtwo.length > 0 ? this.data.classifyListtwo[this.data.twoindex].id : '',
      grade: this.data.rankindex == 1 ? '普通' : this.data.rankindex == 2 ? '中档' : '高档'
    }
    getDoorPrice(data).then(res => {
      let price = -1
      if(res.data.data == -1) {
        price = -1
      } else {
        console.log('else')
        price = res.data.data[0].price
      }
      this.setData({
        price
      })
    })
  },
  getTrulyWeeks() {
    const date = new Date(this.data.days[this.data.dayindex]['appointmentDate'])
    const day = date.getDay()
    const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
    return days[day]
  },
  addressinput(e) {
    this.setData({
      address: e.detail.value
    })
  },
  contactsinput(e) {
    this.setData({
      contacts: e.detail.value
    })
  },
  mobileinput(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  remarkinput(e) {
    this.setData({
      remark: e.detail.value
    })
  },
  createOrderForPay() {
    if(this.data.price == -1) {
      // wx.showToast({
      //   title: '当前项目不存在',
      //   icon: 'none'
      // })
      return
    }
    if(this.data.privincetext == '请选择省' || this.data.privincetext == '') {
      wx.showToast({
        title: '请选择省',
        icon: 'none'
      })
      return
    }
    if(this.data.citytext == '请选择市' || this.data.citytext == '') {
      wx.showToast({
        title: '请选择市',
        icon: 'none'
      })
      return
    }
    if(this.data.areatext == '请选择区' || this.data.areatext == '') {
      wx.showToast({
        title: '请选择区',
        icon: 'none'
      })
      return
    }
    if(this.data.contacts == '') {
      wx.showToast({
        title: '请填写联系人',
        icon: 'none'
      })
      return
    }
    if(this.data.mobile == '') {
      wx.showToast({
        title: '请填写联系电话',
        icon: 'none'
      })
      return
    }
    const data = {
      classifyId: this.data.classifyListOne[this.data.oneindex].id,
      classifyCid: this.data.classifyListtwo[this.data.twoindex].id,
      grade: this.data.rankindex == 1 ? '普通' : this.data.rankindex == 2 ? '中档' : '高档',
      price: this.data.price,
      appointmentTime: this.data.days[this.data.dayindex]['appointmentDate'],
      severalWeeks: this.getTrulyWeeks(),
      timeFrame: this.data.times[this.data.timeIndex]['name'],
      province: this.data.privincetext,
      provinceId: this.data.privinceId,
      city: this.data.citytext,
      cityId: this.data.cityId,
      area: this.data.areatext,
      areaId: this.data.areaId,
      address: this.data.address,
      contacts: this.data.contacts,
      mobile: this.data.mobile,
      imageList: this.data.opinionImages,
      remark: this.data.remark,
      storeId: getApp().globalData.selectStoreId
    }
    createOrder(data).then(res => {
      const orderId = res.data.id
      delayPay({orderId}).then(payres => {
        const paydata = JSON.parse(payres.data.expend.pay_info)
        wx.requestPayment({
          timeStamp: paydata.timeStamp,
          nonceStr: paydata.nonceStr,
          package: paydata.package,
          signType: paydata.signType,
          paySign: paydata.paySign,
          success() {
            wx.navigateTo({
              url: '/pages/doortodoor/success/success?order='+orderId,
            })
          }
        })
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    getTip({id: 143}).then(res => {
      this.setData({
        detailText: res.data.data.configValue
      })
    })
    this.getInitCon()
    const data = {
      pid: 0
    }
    getRegion(data).then(res => {
      const privince = []
      // const city = []
      // const area = []
      res.data.forEach(item => {
        if(item.pid == 0) {
          privince.push(item)
        }
        // if(item.level == 2) {
        //   city.push(item)
        // }
        // if(item.level == 3) {
        //   area.push(item)
        // }
        this.setData({
          privince
          // city,
          // area
        })
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})